<template>
  <el-dialog :title="title" append-to-body :close-on-click-modal="false" :visible.sync="visible" width="700px" @close="clearDataList()" >
    <el-input type="textarea" :rows="4" placeholder="请粘贴配置" v-model="textarea"></el-input>
    <span style="color: red">{{showText}}</span>
    <span slot="footer" class="dialog-footer">
      <el-button type="primary" @click="btnSave()" v-if="submitShow">应用</el-button>
      <el-button @click="clearDataList()">取消</el-button>
    </span>
  </el-dialog>
</template>

<script>
    export default {
    data () {
      return {
        title:'',
        visible: false,
        submitShow:false,
        textarea: '',
        showText:'',

        dataForm: {
          id: 0,
          index: 0,
        },

      }
    },
    methods: {
      setInit(row){
        this.dataForm.id = row.id;
        this.dataForm.index = row.index;
        this.dataForm.mc = row.mc;
      },
      initCreate (row) {
        this.setInit(row);
        this.visible = true;
        this.submitShow = false;
        this.title = "生成配置";
        this.$nextTick(() => {
          this.title += "("+row.mc+")";
          this.showText = "请自行粘贴配置";
          this.textarea = JSON.stringify(row.content);
        })
      },
      initSet(row){
        this.setInit(row);
        this.visible = true;
        this.submitShow = true;
        this.title = "应用配置";
        this.$nextTick(() => {
          this.title += "("+row.mc+")";
        })
      },
      btnSave(){
        if(this.textarea.trim() === ""){
          this.showText = "请粘贴配置";
          return;
        }

        try{
          const sStr = JSON.parse(this.textarea.trim());

          this.$emit("ShowContents",this.textarea.trim(),this.dataForm);
          this.clearDataList();
        }catch (err){
          ////console.log('参数错误')
          this.showText = "参数错误，重新粘贴配置";
        }

      },
      clearDataList(){
        this.textarea = "";
        this.showText = "";
        this.dataForm = {id: 0, index: 0 };
        this.visible = false;
        this.submitShow = false;
      }

    }
  }
</script>
